package example.dao;

import example.domain.User;
import example.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class UserDao {
    //    public boolean insert(User user){}
    public ArrayList<User> findAll(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<User> list = new ArrayList<>();
        try{
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT* FROM users";
            rs= stmt.executeQuery(sql);
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setPassword(rs.getString("password"));
                user.setUsername(rs.getString("name"));
                user.setEmail(rs.getString("email"));
                user.setBirthday(rs.getDate("birthday"));
                list.add(user);
            }
            return  list;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(rs,stmt,conn);
        }
        return  null;
    }
}
